import QtQuick
import QtQuick.Controls.impl
import QtQuick.Templates as T

T.Button {
    id: control

    property color backColor: "#d2d4d6" // 背景颜色
    property color textColor: "#1a1c1e" // 文本颜色
    property color hoverColor: "#919293" // 悬停背景颜色
    property color downColor: "#515253" // 按下背景颜色
    property real animationTime: 250 // 颜色过渡动画时间
    property real itemRadius: control.height / 2 // 圆角
    property real borderWidth: 0 // 边框宽度
    property color borderColor: "#f0f0f0" // 边框颜色
    property real textOffsetX: -255 // 文本x轴偏移 默认水平居中

    implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
                            implicitContentWidth + leftPadding + rightPadding)
    implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
                             implicitContentHeight + topPadding + bottomPadding)

    padding: 6
    horizontalPadding: padding + 2
    spacing: 6

    // 文本内容
    contentItem: Rectangle {
        width: control.width
        height: control.height
        color: "transparent"

        Text {
            id: btnText
            x: textOffsetX === -255 ? (parent.width - width) / 2 : textOffsetX
            y: (parent.height - height) / 2
            text: control.text
            font: control.font
            color: textColor
            verticalAlignment: Text.AlignVCenter
        }
    }
    // 背景
    background: Rectangle {
        implicitWidth: 100
        implicitHeight: 40
        visible: !control.flat || control.down || control.checked || control.highlighted
        color: control.down ? downColor : (control.hovered ? hoverColor : backColor)
        radius: itemRadius
        border.width: borderWidth
        border.color: borderColor

        Behavior on color {

            ColorAnimation {
                duration: animationTime
            }
        }
    }
}
